package com.mustafaiev.tair.cts.dao;

import java.util.List;

import org.apache.commons.lang.NotImplementedException;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.mustafaiev.tair.cts.exeptions.ContextCreationException;
import com.mustafaiev.tair.cts.exeptions.DataNotRemovedException;
import com.mustafaiev.tair.cts.exeptions.DataNotStoredException;
import com.mustafaiev.tair.cts.model.Payer;

/**
 * Payer Data Access Object
 * 
 * @author tair
 * 
 */
public class PayerDAO implements IPayerDAO {

	private static final String DELETE_FROM_PAYER_WHERE_ID = "delete from Payer where id=?";

	private static final String SELECT_ALL_FROM_PAYER = "from Payer";

	private final SessionFactory sessionFactory;

	private static final Logger LOGGER = Logger.getLogger(PayerDAO.class);

	public PayerDAO() throws ContextCreationException {
		try {
			this.sessionFactory = new Configuration().configure()
					.buildSessionFactory();
		} catch (final HibernateException e) {
			throw new ContextCreationException(
					"Hibernate Session was not created", e);
		}
	}

	@Override
	public void doSave(final Payer t) throws DataNotStoredException {

		final Session session = this.sessionFactory.openSession();
		final Transaction transaction = session.getTransaction();
		try {
			transaction.begin();
			session.save(t);
			transaction.commit();
		} catch (final HibernateException e) {
			transaction.rollback();
			LOGGER.error(e);
			throw new DataNotStoredException(
					"Payer was not stored for some reason", e);
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Payer> retrievePayers() {
		final Session session = this.sessionFactory.openSession();
		List<Payer> payers = null;
		try {
			payers = session.createQuery(SELECT_ALL_FROM_PAYER).list();
		} catch (final HibernateException e) {
			LOGGER.error(e);
		}
		return payers;
	}

	@Override
	public Payer retrievePayer(final Long payerId) {
		final Session session = this.sessionFactory.openSession();
		Payer payer = null;
		try {
			payer = (Payer) session.get(Payer.class, payerId);
		} catch (final HibernateException e) {
			LOGGER.error(e);
		}
		return payer;
	}

	@Override
	public void deleteBatch(final List<Payer> payersToDelete)
			throws DataNotRemovedException {
		final Session session = this.sessionFactory.openSession();
		final Transaction transaction = session.getTransaction();
		try {
			transaction.begin();
			for (final Payer payer : payersToDelete) {
				session.createQuery(DELETE_FROM_PAYER_WHERE_ID)
						.setLong(0, payer.getId()).executeUpdate();
			}
			transaction.commit();
		} catch (final HibernateException e) {
			LOGGER.error(e);
			throw new DataNotRemovedException(
					"Payer was not deleted for some reason", e);
		}
	}

	@Override
	public void doDeleteById(final Long id) throws DataNotRemovedException {
		throw new NotImplementedException("doDeleteById() is not implemented");
	}

	@Override
	public void doDelete(final Payer t) throws DataNotRemovedException {
		throw new NotImplementedException("doDelete() is not implemented");
	}

}
